package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Created by mz on 17/5/17.
 * 屠宰检疫的发证的信息实体类
 */
@Data
public class ButcherReportCert implements Serializable{

    private static final long serialVersionUID = 4336101020241707210L;

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    /**
     *  主键ID
     */
    private Long id;

    /**
     * 关联的单据id
     */
    private Long reportId;

    /**
     * 关联的单据的number
     */
    private String reportNumber;

    /**
     * 货主
     */
    private String owner;

    /**
     * 产品名称
     */
    private String productName;

    /**
     * 数量
     */
    private Float quantity;

    /**
     * 单位
     */
    private Integer unit;

    /**
     * 屠宰场id（生产单位）
     */
    private Long butcherFactoryId;

    /**
     * 屠宰场名称（生产单位）
     */
    private String butcherFactoryName;

    /**
     * 屠宰场详细地址（生产单位）
     *
     */
    private String butcherFactoryAddress;

    /**
     *  产地
     */
    private String producingArea;


    private String destinationCode;


    /**
     * 目的地
     */
    private String destination;

    /**
     *  目的地地址
     */
    private String destinationAddress;

    /**
     * 检疫标志号
     */
    private String identificationNumber;

    /**
     * 备注
     */
    private String remarks;

    /**
     * 官方兽医id
     */
    private Long vetUserId;

    /**
     * 官方兽医name
     */
    private String vetUserName;

    /**
     * 屠宰检疫合格证
     */
    private String slaughterQuarantineCertNumber;

    /**
     *
     */
    private Map<String , Map<String , String>> slaughterQuarantineCertPhotoMap;

    /**
     * 屠宰检疫合格证照片
     */
    @JsonIgnore
    private String slaughterQuarantineCertPhoto;

    /**
     *  签发日期
     */
    private Date issuance;

    /**
     * -1 ： 未打印 1: 已打印
     */
    private Integer status;

    private Date createdAt;

    private Date updatedAt;


    public void setSlaughterQuarantineCertPhoto(String slaughterQuarantineCertPhoto){
        this.slaughterQuarantineCertPhoto = slaughterQuarantineCertPhoto;

        if(Strings.isNullOrEmpty(slaughterQuarantineCertPhoto)){
            this.slaughterQuarantineCertPhotoMap = Maps.newHashMap();
        }else {
            try {
                this.slaughterQuarantineCertPhotoMap = objectMapper.readValue(slaughterQuarantineCertPhoto, new TypeReference<Map<String , Map<String , String>>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setSlaughterQuarantineCertPhotoMap(Map<String , Map<String , String>> slaughterQuarantineCertPhotoMap)  {
        this.slaughterQuarantineCertPhotoMap = slaughterQuarantineCertPhotoMap;

        if(slaughterQuarantineCertPhotoMap == null || slaughterQuarantineCertPhotoMap.isEmpty()){
            this.slaughterQuarantineCertPhoto = null;
        }else {
            try {
                this.slaughterQuarantineCertPhoto = objectMapper.writeValueAsString(slaughterQuarantineCertPhotoMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }
}
